(function () {

    YM.namespace('YMW.goods.list');

    $(function () {
        eventBind();
    });

    function eventBind() {
        $('#search-by-order').bind('change', function () {
            window.location.href = this.value;
        });

        $('#display-model-0 .addToCart').click(YMW.goods.list.cartClickEvent);

        $('#ChildMenu1').mouseleave(function () {
            $(this).hide();
        });
        
        //高亮
        if($('#highlighting-keywords').val() !== ''){
        	$('#display-model-1 dt.highlighter-area,#display-model-0 dt.highlighter-area').highlight($('#highlighting-keywords').val());
        }
    }

    YMW.goods.list = {
        AreaCookie: {
            cookieOption: {
                domain: '.yesmywine.com',
                expires: 365,
                path: '/'
            },
            cookieName: "goods_area_name"
        },
        cartClickEvent: function () {
            var index = $(this).attr('statusIndex');
            if ($('#choosed-area-' + index).text() === '区域') {
                alert('请选择区域');
                return false;
            } 
            if($('#choosed-year-' + index + ' span').text() === '选择年份'){
            	alert('请选择年份');
            	return false;
            }
            Cart.addGoodsToChart($(this).attr('goodsId'), 'winenum-' + index, null, null, $(this).attr('yearGoodsId'));
            return false;
        },
        queryGoodsStock: function (goodsId, areaId, areaName, index) {
            var _this = this;
            $.ajax({
                url: '/stock/queryGoodsStock.jspa?goodsId=' + goodsId + '&areaId=' + areaId,
                dataType: 'json',
                complete: function () {
                    $('#stock-loading-' + index).hide();
                },
                beforeSend: function () {
                    $('#stock-loading-' + index).show();
                    $('#stock-tip-' + index).hide();
                    $('#stock-tip2-' + index).hide();
                    $('#stock-error-' + index).hide();
                },
                success: function (data) {
                    if (data.flag) {
                        $('#stock-tip-' + index).html(data.msg).show();
                        $('#buy-' + index).attr('class', 'buy').unbind('click').bind('click', _this.cartClickEvent);
                        
                        //年份
                        //模板中的index值和页面上的index值保持一致
                        if(data.years && data.years.length > 0){
	                        data['index'] = index;
	                        $("#years-list-" + index).empty().show();
	            	        $("#years-tpl2").tmpl(data).appendTo("#years-list-" + index);
	            	        
	            	        //年份默认选中不限
	        		        $('#ol-year-'+index+ ' p a').each(function(){
	        		            if($(this).text() === '不限'){
	        		            	 $(this).click().click();
	        		            }
	        		        });
	            	        
                        }
                    } else {
                        $('#stock-error-' + index).html(data.msg).show();
                        $('#buy-' + index).attr('class', 'buyNone').unbind('click').bind('click', function () {
                            return false;
                        });
                    }
                    $('#choosed-area-' + index + ' span').text(areaName);
                }

            });
        },
        showArea: function (id) {
            var obj = $('#' + id);
            obj.attr('className', 'expanded');
        },

        hideArea: function (id) {
            var obj = $('#' + id);
            obj.attr('className', 'collapsed');
        },
        showOrHideArea: function (id) {
            var obj = $('#' + id);
            obj.attr('className', obj.attr('className').toLowerCase() == "expanded" ? "collapsed" : "expanded");
        },
        //添加到收藏
        addFavorite: function (goodsId) {
            $.ajax({
                type: "POST",
                url: "/mygoods/collectMyGoodsData.jspa",
                data: "goodsId=" + goodsId,
                dataType: 'json',
                success: function (json) {
                    if (json.flag == 0) {
                        showLoginDiv('YMW.goods.list.addFavorite(' + goodsId + ')');
                    } else {
                        alert(json.msg);
                    }
                }
            });
            return false;
        },
        //修改商品数量
        changeGoodsAmount: function (flag, amountId) {
            var txtEl = $('#' + amountId);
            var textValue = $.trim(txtEl.val());
            if (textValue == "") {
                alert("购买数量不能为空！");
                txtEl.focus();
                txtEl.select();
                return;
            }
            if (isNaN(textValue)) {
                alert("数量必须是数字！");
                txtEl.focus();
                txtEl.select();
                return;
            }
            var amount = parseInt(textValue, 10);
            if (amount < 1) {
                alert("数量必须大于0！");
                return;
            }
            if (flag == "add") {
                // 增加数量
                amount = amount + 1;
                txtEl.val(amount);
                return;
            } else if (flag == "sub") {
                // 减少数量
                amount = amount - 1;
                if (amount < 1) {
                    return;
                }
                txtEl.val(amount);
                return;
            } else if (flag == "chg") {
                // 直接修改数量
                if (amount < 1) {
                    alert("数量必须大于0！");
                    txtEl.focus();
                    txtEl.select();
                    return;
                }
            } else {
                return;
            }
            return false;
        },
        setTab: function (name, cursel, n) {
            for (var i = 1; i <= n; i++) {
                var tab = document.getElementById(name + i);
                var con = document.getElementById("con_" + name + "_" + i);
                tab.className = i == cursel ? "hover" : "";
                con.style.display = i == cursel ? "block" : "none";
            }
        }
    };
})();